package com.kaifamiao.object;

// 在Animal类中重写(或覆盖)了从Object继承的三个方法:
// toString() 、equals(Object) 、hashCode
public class Animal {

    private String name;
    private int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object another) {
        if ( another == null ) {
            return false;
        }

        if ( this == another ) {
            return true;
        }

        if( another instanceof Animal ) {
            Animal x = (Animal) another;
            return name != null && name.equals(x.name) && age == x.age;
        }

        return false;
    }

    @Override
    public int hashCode() {
        int result = name == null ? 0 : name.hashCode();
        result = 31 * result + age;
        return result;
    }

    @Override
    public String toString() {
        return String.format("Animal { name: %s, age: %d }", name , age );
    }

    // 被 final 修饰的方法是最终方法
    public final void eat(String food) {
        System.out.printf( "动物%s正在吃%s\n" , name , food );
    }

}
